// script che esegue il vero algoritmo e aggiorna le relative tabelle e i grafici
var query;
var matrix1 = [];
var matrix2 = [];
var matrix3 = [];
var fields1 = [];
var fields2 = [];
var fields3 = [];

function read() {
	query = window.location.search.substr(1);
	query = query.substr(5, 86);
	div = document.getElementById('test');
	//div.innerHTML = query;

	index = 0;
	//var stampaIndex = "";
	for(var y=0; y<9; y++) {
	    matrix1[y] = [];
	    matrix2[y] = [];
	    matrix3[y] = [];
	    fields1[y] = [];
	    fields2[y] = [];
	    fields3[y] = [];
	    for(var x=0; x<9; x++) {
	    	//stampaIndex += "i: " + index + " x: " + x + " y: " + y +"<br/>";
	        var element = query.charAt(index++);
	        if (element != '0') {
	        	matrix1[y][x] = [element];
	        	matrix2[y][x] = [element];
	        	matrix3[y][x] = [element];
	        } else {
	        	matrix1[y][x] = [1,2,3,4,5,6,7,8,9];
	        	matrix2[y][x] = [1,2,3,4,5,6,7,8,9];
	        	matrix3[y][x] = [1,2,3,4,5,6,7,8,9];
	        }
	        
	        fields1[y][x] = document.getElementById("s1"+y+x);
	        fields2[y][x] = document.getElementById("s2"+y+x);
	        fields3[y][x] = document.getElementById("s3"+y+x);
	    }
	}


	//div.innerHTML = stampaIndex;

	/* TEST
	var matrice = "";
	for (var y = 0; y < 9; y++) {
		for (var x = 0; x < 9; x++) {
			matrice += matrix1[y][x] + " ";
		}
		matrice += "<br />";
	}

	matrice += "<br />";
	

	for (var y = 0; y < 9; y++) {
		for (var x = 0; x < 9; x++) {
			matrice += matrix2[y][x] + " ";
		}
		matrice += "<br />";
	}

	matrice += "<br />";
	

	for (var y = 0; y < 9; y++) {
		for (var x = 0; x < 9; x++) {
			matrice += matrix3[y][x] + " ";
		}
		matrice += "<br />";
	}

	div.innerHTML = matrice;*/

	updateTables();

	solver();
}

function updateT1() {
	for (var y = 0; y < 9; y++) {
		for (var x = 0; x < 9; x++) {
			if (matrix1[y][x].length != 1) {
				fields1[y][x].style.fontSize = "0.5em";
				var row = 0;
				var string = "";
				for (var j=0; j<matrix1[y][x].length; j++) {
					if (row > 2) {
						string += "<br/>";
						row = 0;
					}
					string += matrix1[y][x][j] + " ";
					row++;
				}
				fields1[y][x].innerHTML = string;
			} else {
				fields1[y][x].style.fontSize = "2em";
				fields1[y][x].innerHTML = matrix1[y][x];
			}
		}
	}
}

function updateT2() {
	for (var y = 0; y < 9; y++) {
		for (var x = 0; x < 9; x++) {
			if (matrix2[y][x].length != 1) {
				fields2[y][x].style.fontSize = "0.5em";
				var row = 0;
				var string = "";
				for (var j=0; j<matrix2[y][x].length; j++) {
					if (row > 2) {
						string += "<br/>";
						row = 0;
					}
					string += matrix2[y][x][j] + " ";
					row++;
				}
				fields2[y][x].innerHTML = string;
			} else {
				fields2[y][x].style.fontSize = "2em";
				fields2[y][x].innerHTML = matrix2[y][x];
			}
		}
	}
}

function updateT3() {
	for (var y = 0; y < 9; y++) {
		for (var x = 0; x < 9; x++) {
			if (matrix3[y][x].length != 1) {
				fields3[y][x].style.fontSize = "0.5em";
				var row = 0;
				var string = "";
				for (var j=0; j<matrix3[y][x].length; j++) {
					if (row > 2) {
						string += "<br/>";
						row = 0;
					}
					string += matrix3[y][x][j] + " ";
					row++;
				}
				fields3[y][x].innerHTML = string;
			} else {
				fields3[y][x].style.fontSize = "2em";
				fields3[y][x].innerHTML = matrix3[y][x];
			}
		}
	}
}

function updateTables() {
	updateT1();
	updateT2();
	updateT3();
}

function solver() {
	solve1();
	solve2();
	solve3();
	setTimeout(solver, 100);
}

function solve1() {
	var x = Math.floor((Math.random() * 9));
	var y = Math.floor((Math.random() * 9));
	var value = Math.floor((Math.random() * 9) + 1);
	matrix1[y][x].splice(matrix1[y][x].indexOf(value), 1);
	updateT1();
}

function solve2() {
	var x = Math.floor((Math.random() * 9));
	var y = Math.floor((Math.random() * 9));
	var value = Math.floor((Math.random() * 9) + 1);
	matrix2[y][x].splice(matrix2[y][x].indexOf(value), 1);
	updateT2();
}

function solve3() {
	var x = Math.floor((Math.random() * 9));
	var y = Math.floor((Math.random() * 9));
	var value = Math.floor((Math.random() * 9) + 1);
	matrix3[y][x].splice(matrix3[y][x].indexOf(value), 1);
	updateT3();
}